<template>
  <div
    class="page_body"
    v-loading.fullscreen.lock="loadingKey"
    :element-loading-text="loadingText"
  >
    <!-- 面包屑 -->
    <el-row>
      <el-col :span="24" class="page_box_top">
        <i class="el-icon-s-fold"></i>
        <el-breadcrumb separator="/" class="page_box_top_nav">
          <el-breadcrumb-item
            :to="{ name: 'MatchList' }"
            class="breadcrumb_active_item"
            >赛事名称管理</el-breadcrumb-item
          >
          <el-breadcrumb-item class="breadcrumb_active_item"
            ><span @click="matchDetail()" style="cursor: pointer">
              {{ matchtitle }}
            </span></el-breadcrumb-item
          >
          <el-breadcrumb-item class="breadcrumb_active_item"
            ><span @click="departmentDetail()" style="cursor: pointer">
              {{ departmenttitle }}
            </span></el-breadcrumb-item
          >
          <el-breadcrumb-item>单位管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-row>
      <div class="company_manager_content">
        <!-- 按钮栏 -->
        <el-row>
          <el-col :span="24">
            <div class="company_manager_top">
              <el-button size="mini" type="primary" @click="addFn()"
                >新增</el-button
              >
              <el-input
                v-model="searchKey"
                placeholder="请输入关键词"
                size="mini"
                style="width: 200px; margin-left: 10px"
              ></el-input>
              <el-button
                type="primary"
                size="mini"
                style="margin-left: 5px"
                @click="searchFn()"
                >搜索</el-button
              >
            </div>
          </el-col>
        </el-row>
        <!-- 数据列表 -->
        <div
          v-if="companytListData != undefined && companytListData.length > 0"
        >
          <el-row>
            <el-col :span="24">
              <el-table
                :data="companytListData"
                border
                size="mini"
                style="width: 100%"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="50"
                  align="center"
                >
                </el-table-column>

                <el-table-column
                  prop="company_name"
                  label="单位名称"
                  align="center"
                  min-width="200"
                >
                </el-table-column>

                <el-table-column
                  prop="quantity"
                  label="证件数量"
                  width="200"
                  align="center"
                ></el-table-column>

                <el-table-column label="报名二维码" align="center" width="140">
                  <template slot-scope="scope">
                    <el-link
                      type="primary"
                      @click="companyCodeFn(scope.$index, scope.row)"
                      >点击查看</el-link
                    >
                  </template>
                </el-table-column>

                <el-table-column label="证件种类" align="center" width="140">
                  <template slot-scope="scope">
                    <el-link
                      type="primary"
                      @click="passListFn(scope.$index, scope.row)"
                      >点击查看</el-link
                    >
                  </template>
                </el-table-column>

                <el-table-column
                  prop="created_at"
                  label="创建时间"
                  width="200"
                  align="center"
                ></el-table-column>

                <el-table-column label="操作" width="200" align="center">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="success"
                      @click="editFn(scope.$index, scope.row)"
                      :disabled="scope.row.release_status == 1"
                      >编辑</el-button
                    >

                    <el-button
                      size="mini"
                      type="danger"
                      @click="deleteFn(scope.$index, scope.row)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="company_manager_pagination">
                <el-pagination
                  @size-change="listSizeChange"
                  @current-change="listCurrentChange"
                  :current-page="currentPage"
                  :page-sizes="pageSizes"
                  :page-size="pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total"
                >
                </el-pagination>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-row v-else>
          <el-empty description="暂无数据"></el-empty>
        </el-row>
        <!-- 弹窗 -->
        <el-dialog
          :title="dialogTitle"
          :visible.sync="dialogVisible"
          :close-on-click-modal="false"
          width="500px"
        >
          <el-form
            ref="dialogDetailForm"
            :model="detailFormData"
            :rules="detailFormRules"
            v-if="dialogBtnKey == 1 || dialogBtnKey == 2"
          >
            <el-form-item
              label="单位名称"
              prop="company_name"
              :label-width="formLabelWidth"
            >
              <el-input
                size="small"
                v-model="detailFormData.company_name"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="证件数量"
              prop="quantity"
              :label-width="formLabelWidth"
            >
              <el-input
                type="number"
                size="small"
                v-model="detailFormData.quantity"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="备注"
              prop="remark"
              :label-width="formLabelWidth"
            >
              <el-input size="small" v-model="detailFormData.remark"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="dialogclose()">取 消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="dialogDetailFormSubmit()"
              >确 定</el-button
            >
          </span>
        </el-dialog>

        <!-- 证件种类列表 -->
        <el-dialog
          title="证件种类"
          :visible.sync="passListDialogVisible"
          :close-on-click-modal="false"
          width="500px"
        >
          <el-row>
            <el-col :span="24">
              <el-table
                :data="passListData"
                border
                size="mini"
                style="width: 100%"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="50"
                  align="center"
                >
                </el-table-column>

                <el-table-column
                  prop="pass_name"
                  label="证件种类"
                  align="center"
                  min-width="200"
                >
                </el-table-column>

                <el-table-column label="报名二维码" align="center" width="140">
                  <template slot-scope="scope">
                    <el-link
                      type="primary"
                      @click="getCodeFn(scope.$index, scope.row)"
                      >点击查看</el-link
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
          <span slot="footer" class="dialog-footer">
            <el-button size="small" type="primary" @click="passDialogclose()"
              >确 定</el-button
            >
          </span>
        </el-dialog>

        <!-- 二维码弹窗 -->
        <el-dialog
          title="报名二维码"
          :visible.sync="codeDalogVisible"
          :close-on-click-modal="false"
          width="400px"
        >
          <div ref="qrCode_box" class="card_data_body_qrcode"></div>
          <div class="qrCode_link">链接: {{ qrcodeUrl }}</div>
          <span slot="footer" class="dialog-footer">
            <el-button size="small" type="primary" @click="copy(qrcodeUrl)"
              >复制链接</el-button
            >
            <el-button size="small" @click="codeDialogclose()">关 闭</el-button>
          </span>
        </el-dialog>
      </div>
    </el-row>
  </div>
</template>
<style src="../../assets/css/company/companyManager.css" scoped></style>

<script>
import QRCode from "qrcodejs2";

export default {
  name: "CompanyManager",
  components: {},
  data() {
    return {
      userType: this.$store.state.loginUserType == "ADMINLOGINUSERCH" ? 0 : 1, // 用户类型;1. 普通; 0. 超级;
      baseAPIUrl: this.$store.state.baseAPIUrl,
      matchtitle: "", // 赛事名称名称
      matchId: "", // 赛事名称id
      departmentId: "", // 部门id
      departmenttitle: "", // 部门
      departmentMark: "", // 部门mark
      companytListData: [], // 数据列表
      currentPage: 1, // 当前页
      pageSizes: [10, 20, 50, 100], // 可选页容量
      pageSize: 10, // 当前页容量
      total: 0, // 总条数
      searchKey: "", // 搜索关键词
      loadingKey: false,
      loadingText: "",
      dialogTitle: "", // 弹窗标题
      dialogBtnKey: "", // 弹窗确认按钮控制; 1. 新增公司确认;2. 修改公司确认;
      dialogVisible: false, // 弹窗显示
      detailFormData: {
        id: "",
        event_department_id: "",
        company_name: "",
        quantity: "",
        remark: "",
      }, // 公司弹窗数据
      detailFormRules: {
        company_name: [
          { required: true, message: "请输入单位名称", trigger: "blur" },
          {
            min: 2,
            max: 12,
            message: "长度在 2 到 12 个字符",
            trigger: "blur",
          },
        ],
        quantity: [
          { required: true, message: "请输入证件数量", trigger: "blur" },
        ],
      },
      formLabelWidth: "90px",
      passListData: [], // 证件种类列表
      passListDialogVisible: false, // 证件种类列表弹窗显示
      passListCompanyMark: "", // 证件种类列表弹窗公司id
      codeDalogVisible: false, // 二维码弹窗显示
      qrcodeUrl: "", // 二维码链接
    };
  },
  created: function () {
    let departmentId = this.$route.query.id;
    if (departmentId != undefined && departmentId != "") {
      this.departmentId = departmentId;
    }

    this.getListDataFn();
  },
  methods: {
    // 获取列表数据
    getListDataFn: function () {
      let that = this;
      let url = "/admin/company/index";
      let data = {
        event_department_id: this.departmentId,
        page: this.currentPage,
        perPage: this.pageSize,
        keywords: this.searchKey,
      };
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            that.departmenttitle = response.department_name;
            that.departmentMark = response.department_mark;
            that.matchtitle = response.event_title;
            that.matchId = response.event_id;
            that.companytListData = response.list;
            that.total = response.total_count;
            that.getPassListDataFn();
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 获取证件种类列表数据
    getPassListDataFn: function () {
      let that = this;
      let url = "/admin/pass/index";
      let data = {
        event_id: this.matchId,
      };
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            that.passListData = response.list;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },
    // 新增公司
    addFn: function () {
      let that = this;
      this.dialogTitle = "新增单位";
      this.dialogBtnKey = 1;
      this.detailFormData = {
        id: "",
        event_department_id: this.departmentId,
        company_name: "",
        quantity: "",
        remark: "",
      };
      this.$nextTick(function () {
        that.dialogVisible = true;
        that.$refs.dialogDetailForm.clearValidate();
      });
    },
    // 修改公司
    editFn: function (index, row) {
      this.dialogTitle = "编辑单位";
      this.dialogBtnKey = 2;
      this.detailFormData = {
        id: row.id,
        event_department_id: this.departmentId,
        company_name: row.company_name,
        quantity: row.quantity,
        remark: row.remark,
      };
      this.$nextTick(function () {
        this.dialogVisible = true;
      });
    },
    // 公司保存按钮
    dialogDetailFormSubmit: function () {
      let that = this;
      let url = "";
      let data = {};
      if (this.dialogBtnKey == 2) {
        url = "/admin/company/update";
        data = {
          id: this.detailFormData.id,
          company_name: this.detailFormData.company_name,
          quantity: this.detailFormData.quantity,
          remark: this.detailFormData.remark,
        };
      } else {
        url = "/admin/company/store";
        data = {
          event_department_id: this.detailFormData.event_department_id,
          company_name: this.detailFormData.company_name,
          quantity: this.detailFormData.quantity,
          remark: this.detailFormData.remark,
        };
      }
      this.$refs.dialogDetailForm.validate((valid) => {
        if (!valid) {
          return false;
        } else {
          that
            .$confirm("确认提交?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
            })
            .then(() => {
              that.loadingText = "处理中...";
              that.loadingKey = true;
              that.axios
                .post(url, data)
                .then((response) => {
                  that.loadingKey = false;
                  if (response.code == 0) {
                    if (that.dialogBtnKey == 2) {
                      that.$message.success("修改成功");
                    } else {
                      that.$message.success("新增成功");
                    }
                    that.dialogclose();
                    that.getListDataFn();
                  } else {
                    that.$message.error(response.msg);
                  }
                })
                .catch(function (error) {
                  that.loadingKey = false;
                  console.log(error);
                });
            })
            .catch(() => {});
        }
      });
    },

    // 关闭弹窗
    dialogclose: function () {
      this.dialogVisible = false;
    },

    // 删除
    deleteFn: function (index, row) {
      let that = this;
      this.$confirm("确认删除该单位?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          let url = "/admin/company/destroy";
          let data = {
            id: row.id,
          };
          that.loadingText = "处理中...";
          that.loadingKey = true;
          that.axios
            .post(url, data)
            .then((response) => {
              that.loadingKey = false;
              if (response.code == 0) {
                that.$message.success("删除成功");
                let next = that.currentPage - 1;
                if (that.companytListData.length == 1) {
                  that.currentPage = next >= 1 ? next : 1;
                }
                that.getListDataFn();
              } else {
                that.$message.error(response.msg);
              }
            })
            .catch(function (error) {
              that.loadingKey = false;
              console.log(error);
            });
        })
        .catch(() => {});
    },
    // 页容量改变
    listSizeChange: function (val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getListDataFn();
    },
    // 当前页改变
    listCurrentChange: function (val) {
      this.currentPage = val;
      this.getListDataFn();
    },
    // 搜索按钮
    searchFn: function () {
      this.currentPage = 1;
      this.getListDataFn();
    },
    // 关闭证件种类弹窗
    passDialogclose: function () {
      this.passListDialogVisible = false;
    },
    // 打开证件种类弹窗
    passListFn: function (index, row) {
      this.passListDialogVisible = true;
      this.passListCompanyMark = row.mark;
    },
    // 关闭二维码弹窗
    codeDialogclose: function () {
      this.codeDalogVisible = false;
    },
    // 复制文本
    copy: function (data) {
      let OrderNumber = data;
      // 创建一个input 元素
      let newInput = document.createElement("input");
      newInput.value = OrderNumber;
      document.body.appendChild(newInput);
      newInput.select();
      document.execCommand("Copy");
      newInput.remove();
      that.$message.success("复制成功");
    },
    // 报名二维码
    getCodeFn: function (index, row) {
      let that = this;
      this.codeDalogVisible = true;
      // let dataObj = {
      //   mark: that.departmentMark,
      //   companyId: that.passListCompanyMark,
      //   passId: row.id,
      // }
      this.$nextTick(() => {
        that.$refs.qrCode_box.innerHTML = "";
        that.qrcodeUrl =
          that.$store.state.baseUrl +
          "login?mark=" +
          that.departmentMark +
          "&company=" +
          that.passListCompanyMark +
          "&pass=" +
          row.id;
        that.createQRCodeFn(that.qrcodeUrl).then(() => {});
      });
    },
    // 生成二维码
    createQRCodeFn: function (url) {
      let that = this;
      return new Promise(function (resolve) {
        setTimeout(function () {
          // let text = that.$store.state.baseUrl + "login?mark=" + obj.mark;
          let text = url;
          let qrcodejs2 = new QRCode(that.$refs.qrCode_box, {
            text: JSON.stringify(text), //二维码内容
            colorDark: "#000000",
            colorLight: "#ffffff",
          });
          resolve(qrcodejs2);
        }, 1000);
      });
    },
    // 公司二维码
    companyCodeFn: function (index, row) {
      let that = this;
      this.codeDalogVisible = true;
      this.passListCompanyMark = row.mark;
      this.$nextTick(() => {
        that.$refs.qrCode_box.innerHTML = "";
        that.qrcodeUrl =
          that.$store.state.baseUrl +
          "login?mark=" +
          that.departmentMark +
          "&company=" +
          that.passListCompanyMark;
        that.createQRCodeFn(that.qrcodeUrl).then(() => {});
      });
    },
  },
};
</script>
